package com.hnyfkj.jyindustry.biz.login.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnyfkj.jyindustry.biz.login.dto.ChangePasswordDto;
import com.hnyfkj.jyindustry.biz.parameter.service.ParameterService;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.entity.SysMemberInfoEntity;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.sevice.SysMemberInfoService;
import com.hnyfkj.jyindustry.common.utils.Response;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月9日
 */
@Controller
@Api(tags = { "登录以及首页相关" })
public class LoginController {
	@Autowired
	SysMemberInfoService sysMemberInfoService;
	@Autowired
	ParameterService parameterService;

	/**
	 * 跳转到登录页面
	 */
	@GetMapping("/")
	public String toLogin() {
		return "login";
	}

	@PostMapping("/login")
	@ResponseBody
	@ApiOperation(value = "登录")
	public Response login(@RequestParam("username")String username,@RequestParam("password")String password) throws LockedAccountException {
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		try {
			token.setRememberMe(true);
			// 触发 shiro 检查密码逻辑
			currentUser.login(token);
			return Response.success();
		} catch (LockedAccountException e) {
			return Response.error("该账户已禁用，请联系管理员！");
		} catch (Exception e) {
			return Response.error("用户名或密码错误！");
		}
	}
	
	/**
	 * 跳转到首页面
	 */
	@PostMapping("/")
	public String index(Model model) {
		// 返回用户信息
		String memberId = (String) ShiroUtils.getSessionAttribute("memberId");
		SysMemberInfoEntity sysMemberInfoEntity = sysMemberInfoService.getById(memberId);
		model.addAttribute("username", sysMemberInfoEntity.getUserName());
		// 获取上传图片路径
		String uploadUrl = parameterService.getSysParameter("fileUpload");
		model.addAttribute("uploadUrl", uploadUrl);
		model.addAttribute("barbarName", sysMemberInfoEntity.getSystemName());
		model.addAttribute("memberId", sysMemberInfoEntity.getMemberId());
		model.addAttribute("roleType", sysMemberInfoEntity.getRoleType());
		return "index";
	}

	@PostMapping("/logout")
	@ResponseBody
	public Response logout() {
		ShiroUtils.logout();
		return Response.success();
	}
	
	/**
	 * 修改密码
	 */
    @PostMapping("/changePassword")
	@ApiOperation(value = "修改密码")
    @ResponseBody
	public Response changePassword(@RequestBody final ChangePasswordDto changePasswordDto) {
    	return sysMemberInfoService.changePassword(changePasswordDto);
	}
}
